# -*- coding: utf-8 -*-
# @Author: Administrator
# @Date:   2022-05-19 16:05:24
# @Last Modified by:   Administrator
# @Last Modified time: 2022-06-16 09:49:20
from copy import deepcopy
from flask import Blueprint, request, jsonify
from controllers.common import *
from controllers.db import *

buy_pid_reward_send_api = Blueprint('buy_pid_reward_send_api', __name__)
buy_reward_pid_list_api = Blueprint('buy_reward_pid_list_api', __name__)
buy_pid_reward_send_down_api = Blueprint('buy_pid_reward_send_down_api', __name__)

@buy_pid_reward_send_api.route("/buy_pid_reward_send", methods=['POST', 'GET'])
def project_action():
    update_data = json.loads(request.data)
    _type = update_data['type']
    project = update_data['project']

    result = {'msg': '成功', 'code': 200, 'data':[]}
    if project == -1:
        project_data = BuyPidRewardSend.query.order_by('project').all()
    else:
        project_data = BuyPidRewardSend.query.filter_by(project=project).first()

    if _type == 'index':
        for d in project_data:
            _project = d.project
            basic = json.loads(d.data)
            basic['project'] = _project
            result['data'].append(basic)

        reward_pid_list = []
        reward_query = BuyRewardPidList.query.order_by('project').all()
        for reward_que in reward_query:
            reward_pid = reward_que.project
            reward_data = json.loads(reward_que.data)
            remark = reward_data['remark']
            tmp = {'label': '{0}-{1}'.format(reward_pid, remark),
                   'value': reward_pid}
            reward_pid_list.append(tmp)

        result['reward_pid_list'] = reward_pid_list

    elif _type == 'delete':
        db.session.delete(project_data)
        db.session.commit()
        new_project_data = BuyPidRewardSend.query.filter_by(project=project).first()
        if new_project_data is None:
            result = {'msg': '删除成功', 'code': 0}
        else:
            result = {'msg': '删除失败', 'code': 1}

    elif _type == 'update':
        data_list = update_data['data_list']
        for node in data_list:
            if node['project'] != -1:
                sch_que = BuyPidRewardSend.query.filter_by(
                    project=node['project']).first()
                sch_que.data = json.dumps(node)
            else:
                new_node = BuyPidRewardSend(data=json.dumps(node))
                db.session.add(new_node)
            db.session.commit()

    return jsonify(result) 

@buy_reward_pid_list_api.route("/buy_reward_pid_list", methods=['POST', 'GET'])
def project_reward_action():
    result = {'msg': '成功', 'code': 200, 'data':[]}
    if request.method == 'GET':
        project_data = BuyRewardPidList.query.order_by('project').all()
        for d in project_data:
            _project = d.project
            basic = json.loads(d.data)
            basic['project'] = _project
            result['data'].append(basic)
        return jsonify(result)

    update_data = json.loads(request.data)
    _type = update_data['type']
    project = update_data['project']
    
    if _type == 'delete':
        project_data = BuyRewardPidList.query.filter_by(project=project).first()
        db.session.delete(project_data)
        db.session.commit()
        new_project_data = BuyRewardPidList.query.filter_by(project=project).first()
        if new_project_data is None:
            result = {'msg': '删除成功', 'code': 0}
        else:
            result = {'msg': '删除失败', 'code': 1}

    elif _type == 'update':
        data_list = update_data['data_list']
        for node in data_list:
            if node['project'] != -1:
                sch_que = BuyRewardPidList.query.filter_by(
                    project=node['project']).first()
                sch_que.data = json.dumps(node)
            else:
                new_node = BuyRewardPidList(data=json.dumps(node))
                db.session.add(new_node)
            db.session.commit()

    return jsonify(result)

@buy_pid_reward_send_down_api.route("/buy_pid_reward_send_down/<int:is_push>", methods=['POST', 'GET'])
def DownAction(is_push):
    result = []

    query = BuyPidRewardSend.query.order_by('project').all()

    for p in query:
        project_id = str(p.project)
        data = json.loads(p.data)
        reward_pid = data['reward_pid']
        reward_que = BuyRewardPidList.query.filter_by(project=reward_pid).first()
        reward_data = json.loads(reward_que.data)
        reward = RewardToTMysqlNew(reward_data['reward'])

        tmp = {'id': int(project_id),
               'begin_time': fomat_date_str(data['begin_time']),
               'end_time': fomat_date_str(data['end_time']),
               'buy_times': data['buy_times'],
               'trigger_times': data['trigger_times'],
               'pid': data['pid'],
               'mail_doc_id': data['mail_doc_id'],
               'reward': reward}
        result.append(tmp)

    response = DownLoad('buy_pid_reward_send', result, is_push)

    return response
